using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utils.Extention;

namespace ChinaLeg.Web.WebAdmin.News
{
    public partial class NewsList : ChinaLeg.Web.Lib.PageBase
    {
        protected string CatName;
        protected int CatId;
        protected void Page_Load(object sender, EventArgs e)
        {
            CatId = Request.QueryString["id"].ToInt32();
            CatName = Request.QueryString["name"].ToTrim();
            txtDate.Attributes["onclick"] = "__Calendar__.show(this, {})";
            if (!this.IsPostBack)
            {
                BindList();
                
            }
        }
        public void BindList()
        {
            string where = " and catid="+CatId;

            switch (ddlCommend.SelectedValue)
            {
                case "1":
                    where += " and IndexCommend=1 ";
                    break;
                case "2":
                    where += " and ListCommend=1 ";
                    break;
                case "3":
                    where += " and DetailsCommend=1 ";
                    break;
            }

            if (!Request.Form["txtDate"].ToTrim().IsNullOrEmpty())
            {
                where += string.Format(" and MidifyTime between '{0}' and '{0} 23:59:59' ", Request.Form["txtDate"]);
            }

            if (!txtTitle.Text.ToTrim().IsNullOrEmpty())
            {
                where += string.Format(" and Title like '%{0}%' ", txtTitle.Text );
            }
            
            int recordCount;
            gvData.DataSource = ChinaLeg.BLL.News.Instance.GetList(where, aspNetPager.PageSize, aspNetPager.CurrentPageIndex, out recordCount);
            gvData.DataBind();
            aspNetPager.RecordCount = recordCount;
        }
        protected void aspNetPager_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }
        protected void gvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string cmd = e.CommandName.ToLower();
            int id = e.CommandArgument.ToInt32();
            switch (cmd)
            {
                case "del":
                    ChinaLeg.BLL.News.Instance.Delete(id);
                    break;
                default:
                    break;
            }
            BindList();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            aspNetPager.CurrentPageIndex = 1;
            BindList();

        }

        protected void btnOrder_Click(object sender, EventArgs e)
        {
            int len = gvData.Rows.Count;
            for (int i = 0; i < len; i++)
            {
                int id = gvData.Rows[i].Cells[0].Text.ToInt32();
                int order = (gvData.Rows[i].FindControl("txtOrder") as TextBox).Text.ToInt32();
                ChinaLeg.BLL.News.Instance.Update(id, order);
            }
            BindList();
        }

    }
}